<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once '../config.php';

include_once ABSPATH."/base/curl.php";


class People {
	
   function Query($q = '', $offset='0', $rowsPerPage = '20', $total=false) {
    	
   	    global $db;
    	
   	    $query = "select ";
   	    
   	    if($total)
   	    {
    		$query .= "count(*) ";
   	    }
   	    else
   	    {
   	    	$query .= "* ";
   	    }
   	    
   	    $query .= "from $db->people ";
   	     
    	if(strlen($q) > 0)
    	{
    		$query .="where name like '%".$q."%' or screen_name like '%".$q."%' ";

    	}
    	if(strlen($rowsPerPage) == 0)
    	{
    		$rowsPerPage = '20';
    	}
    	
    	if($total)
    	{
    		$ret = $db->get_var($query);
    	}
    	else
    	{
    		$query .= " order by name limit $offset,$rowsPerPage";
    		$ret = $db->get_results($query,ARRAY_A);
    	}

    	return $ret;
   }	
	

}

class Person {
   
   var $id="";
   var $name="";
   var $screen_name="";
   var $email="";
   var $address="";
   var $image="";
   var $update_address = "";
   var $ext_id = "";
   var $last_check;
   var $last_update;
   
   function Person($id='')
   {
   	   if(strlen($id) > 0)
   	   {
   	   	    $this->id = $id;
   	   		$this->Get();
   	   }
   }

   function Create($oid) {
    	global $db;
    	$id = "";
    	
    	$rs = $db->query("insert into $db->people (ext_id) values('$oid')");
    	if($rs)
    	{
    		$id = $db->insert_id;
    		$this->id = $id;
    		$this->address = HOME."users/?id=" . $id;    
    		$this->last_update = "1900-01-01";
    		$this->last_check = "1900-01-01";		
    		$this->Save();
	    	$this->Connect($id);
	    	
    	}
    	
		return $id;
   }
   
   function CreateExt($address) {
    	global $db;
    	$id = "";
    	
    	$curl = new cURL();
    	if(strpos($address,"http://") == false && strpos($address,"http://") != 0)
    	{
    		$address = "http://".$address;
    	}
    	$url = $address."&cmd=profile.xml";
	    $xml = $curl->get($url);
    	$doc = new DOMDocument();
    	$doc->loadXML($xml);
    	foreach($doc->getElementsByTagName('user') as $user)
    	{
    		foreach($user->childNodes as $node)
    		{
    			switch ($node->nodeName)
    			{
    				case "name":
    					$name = $node->nodeValue;
    					break;
    				case "screen_name":
    					$screen_name = $node->nodeValue;
    					break;
    			}
    		}
    	}

    	$rs = $db->query("insert into $db->people (address,name,screen_name, last_update, last_check) values('$address','".$db->escape($name)."','".$db->escape($screen_name)."','1900-01-01','1900-01-01')");
    	if($rs)
    	{
    		$id = $db->insert_id;
    	}
    	
		return $id;
   }
   

   function Save() {
    	global $db;
    	
		$image = HOME.'images/nerds.jpg';
		if(strlen($this->image) == 0)
		{
			$this->image = $image;
		}
    	
    	
    	$result = $db->query("update $db->people set name='".$db->escape($this->name)."', screen_name='".$db->escape($this->screen_name)."', email='".$db->escape($this->email)."' "
    	       ." ,image='".$db->escape($this->image)."', address='".$db->escape($this->address)."' "
    	       ." ,last_update='".$this->last_update."',last_check='".$this->last_check."' "
    	       ."where id='$this->id'");

		return $result;
   }
   
      
   function Get() {
    	global $db;
    	$row = $db->get_row("select id, name, screen_name, image, email, address, ext_id, last_check, last_update from $db->people where id='$this->id'",ARRAY_A);
        if($row)
        {
	    	$this->id=$row["id"];
	    	$this->image=$row["image"];    		
	    	$this->name=$row["name"];
	    	$this->screen_name=$row["screen_name"];
	    	$this->email=$row["email"];
	    	$this->address=$row["address"];
	    	$this->ext_id=$row["ext_id"];    		    		    		
	    	$this->last_check=$row["last_check"];
	    	$this->last_update=$row["last_update"];
        }	
    	return $row;
   }
   
   public static function UIDFromOID($oid)
   {
    	global $db;
        $id = "";
    	if(!empty($oid))
        {
  	    	$id = $db->get_var("select id from $db->people where ext_id='$oid'");
        }
    	return $id;   	
   	
   }
   
   public static function IDFromScreenName($screen_name)
	{
	    global $db;
	    $id = $db->get_var("select id from $db->people where screen_name='$screen_name'");
	    return $id;
		
	}   

	public static function IDFromAddress($address)
	{
	    global $db;
	    $id = $db->get_var("select id from $db->people where address='$address'");
	    return $id;
		
	}   

	function PostSecret($address)
	{
	    global $db;
	    $secret = $db->get_var("select secret from $db->connections A, $db->people B where A.contact_id=B.id and A.ownerid='$this->id' and B.address='$address'");
	    if(strlen($secret) == 0)
	    {
	    	$secret = uuid();
	    }
	    $curl = new cURL();
	    if(strpos($address,"?") == false)
	    {
	    	$address .= "?";
	    }
	    else
	    {
	    	$address.="&";
	    }
	    $url = $address."cmd=secret.post&me=".$this->address."&secret=".$secret;
	    $curl->post();
	}   
	function StoreSecret($contact_id, $secret)
	{
		global $db;		
	    $ok = $db->query("update $db->connections set secret='$secret' where ownerid='$this->id'and contact_id='$contact_id'");        
		
	}   
    function ConnectionsGet($offset='0', $rowsPerPage = '20', $total=false) {
    	global $db;
    	
  	    $query = "select ";
   	    
   	    if($total)
   	    {
    		$query .= "count(*) ";
   	    }
   	    else
   	    {
   	    	$query .= "B.id, C.channel_id,B.contact_id,A.name, A.address ";
   	    }
   	    
   	    $query .= "from $db->people A, $db->connections B, $db->cnxn_channels C ";
   	     
   		$query .="where A.id=B.contact_id and B.id=C.cnxn_id and B.ownerid='$this->id' ";

    	if(strlen($rowsPerPage) == 0)
    	{
    		$rowsPerPage = '20';
    	}
    	
    	if($total)
    	{
    		$ret = $db->get_var($query);
    	}
    	else
    	{
    		$query .= "order by B.contact_id, C.channel_id limit $offset,$rowsPerPage";
    		$ret = $db->get_results($query,ARRAY_A);
    	}
    	
    	return $ret;
    }
    function ConnectionsGetByFrequency() {
    	global $db;
    	$rs = $db->get_results("select B.id, B.contact_id,A.name, A.address, A.last_update from $db->people A, $db->connections B where A.id=B.contact_id and B.ownerid='$this->id' order by A.last_update desc limit 5 ",ARRAY_A);
    	return $rs;
    }
    function ConnectionsGetByUpdateCheck() {
    	global $db;
    	$rs = $db->get_results("select B.id, B.contact_id,A.name, A.address, A.last_update from $db->people A, $db->connections B where A.id=B.contact_id and B.ownerid='$this->id' order by A.last_check limit 5 ",ARRAY_A);
    	return $rs;
    }            
    function ConnectionGet($contact_id,$channel_id='1')
    {
    	global $db;
    	$rs = $db->get_row("select A.id,A.ownerid,A.contact_id,B.channel_id from $db->connections A, $db->cnxn_channels B where A.ownerid='$this->id' and A.contact_id='$contact_id' and B.cnxn_id=A.id and B.channel_id='$channel_id'", ARRAY_A);
    	return $rs;
    	
    }
    
    function ChannelsGet($contact_id)
    {
    	global $db;
    	$rs = $db->get_results("select B.channel_id from $db->connections A, $db->cnxn_channels B where A.ownerid='$this->id' and A.contact_id='$contact_id' and B.cnxn_id=A.id", ARRAY_A);
    	return $rs;
    	
    }    
    
    function Connect($contact_id, $channel_id='1') {
    	global $db;

    	$rs = $db->get_row("select id from $db->connections where ownerid='$this->id' and contact_id='$contact_id'",ARRAY_A);
    	
    	if($rs)
    	{
    		$cnxn_id = $rs["id"];
    	}
    	    	
    	if($db->num_rows <= 0)
    	{
	    	$ok = $db->query("insert into $db->connections (ownerid, contact_id) values('$this->id','$contact_id')");
	    	$ok = $db->query("insert into $db->cnxn_channels (cnxn_id, channel_id) values('$db->insert_id','$channel_id')");	    	
	    	
    	}
    	else
    	{

	    	$rs = $db->get_row("select cnxn_id from $db->cnxn_channels where cnxn_id='$cnxn_id' and channel_id='$channel_id'",ARRAY_A);
	    	if($db->num_rows <= 0)
	    	{
		    	$ok = $db->query("insert into $db->cnxn_channels (cnxn_id, channel_id) values('$cnxn_id','$channel_id')");
	    	}
    	}	
    	
    	return $ok;
    }
    
    function Disconnect($contact_id, $channel_id='1') {
    	global $db;
    	
    	$rs = $db->get_row("select id from $db->connections where ownerid='$this->id' and contact_id='$contact_id'",ARRAY_A);
    	
    	if($rs)
    	{
    		$cnxn_id = $rs["id"];
    	}    	
    	
    	$rs = $db->get_row("select cnxn_id from $db->cnxn_channels A where cnxn_id='$cnxn_id' and channel_id='$channel_id'");
    	
    	if($db->num_rows > 0)
    	{
	    	$ok = $db->query("delete from $db->cnxn_channels where cnxn_id='$cnxn_id' and channel_id='$channel_id'");
    	}
    	
    	$rs = $db->get_row("select cnxn_id from $db->cnxn_channels where cnxn_id='$cnxn_id'");
    	if($db->num_rows <= 0)
		{
    		$ok = $db->query("delete from $db->connections where ownerid='$this->id' and contact_id='$contact_id'");
    	}
	
    	return $ok;
    }    
        
    
	

}

?>